<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.text" placeholder="Account" clearable style="width: 180px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-input v-model="listQuery.description" placeholder="Description" clearable style="width: 180px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-select v-model="listQuery.dictGroup" placeholder="group" filterable clearable style="width: 130px" class="filter-item">
        <el-option v-for="item in dictGroups" :key="item.code" :label="item.text" :value="item.code" />
      </el-select>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">Search</el-button>
      <!-- show column popover -->
      <el-popover placement="bottom" width="150" trigger="click">
        <el-checkbox v-model="showRows.code" class="filter-item" @change="tableKey = tableKey + 1">Code</el-checkbox>
        <el-checkbox v-model="showRows.textColor" class="filter-item" @change="tableKey = tableKey + 1">Text Color</el-checkbox>
        <el-checkbox v-model="showRows.reserved1" class="filter-item" @change="tableKey = tableKey + 1">Reserved1</el-checkbox>
        <el-checkbox v-model="showRows.reserved2" class="filter-item" @change="tableKey = tableKey + 1">Reserved2</el-checkbox>
        <el-checkbox v-model="showRows.enabled" class="filter-item" @change="tableKey = tableKey + 1">Enabled</el-checkbox>
        <el-checkbox v-model="showRows.defaulted" class="filter-item" @change="tableKey = tableKey + 1">Defaulted</el-checkbox>

        <el-button slot="reference" v-waves class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-setting" />
      </el-popover>

      <router-link :to="{name:'CreateDictionary'}">
        <el-button v-waves class="filter-item" type="primary" plain icon="el-icon-plus" />
      </router-link>
    </div>

    <el-table :key="tableKey" v-loading="listLoading" :data="list" border fit highlight-current-row style="width: 100%" @sort-change="sortChange">
      <el-table-column align="center" label="ID" width="80">
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="Text">
        <template slot-scope="{ row }">
          <span>{{ row.text }}</span>
        </template>
      </el-table-column>

      <el-table-column v-if="showRows.code" align="center" label="Code">
        <template slot-scope="scope">
          <span>{{ scope.row.code }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="Group">
        <template slot-scope="scope">
          <span>{{ scope.row.dictGroup }}</span>
        </template>
      </el-table-column>

      <el-table-column v-if="showRows.textColor" align="center" label="Text Color">
        <template slot-scope="scope">
          <span>{{ scope.row.textColor }}</span>
        </template>
      </el-table-column>

      <el-table-column v-if="showRows.reserved1" align="center" label="Reserved1">
        <template slot-scope="scope">
          <span>{{ scope.row.reserved1 }}</span>
        </template>
      </el-table-column>

      <el-table-column v-if="showRows.reserved2" align="center" label="Reserved2">
        <template slot-scope="scope">
          <span>{{ scope.row.reserved2 }}</span>
        </template>
      </el-table-column>

      <el-table-column v-if="showRows.enabled" align="center" label="Enabled">
        <template slot-scope="scope">
          <span>{{ scope.row.enabled }}</span>
        </template>
      </el-table-column>

      <el-table-column v-if="showRows.defaulted" align="center" label="Defaulted">
        <template slot-scope="scope">
          <span>{{ scope.row.defaulted }}</span>
        </template>
      </el-table-column>

      <el-table-column width="180px" align="center" prop="modify_at" sortable="custom" label="UpdateTime">
        <template slot-scope="scope">
          <span>{{ scope.row.modifyAt | parseTime('{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>

      <el-table-column fixed="right" label="Actions" width="120">
        <template slot-scope="scope">
          <router-link :to="{name: 'EditDictionary', params: scope.row }">
            <el-button type="primary" size="mini" icon="el-icon-edit" plain />
          </router-link>
          <el-button type="danger" size="mini" icon="el-icon-delete" plain @click="handleDelete(scope.row.id)" />
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.current" :limit.sync="listQuery.size" @pagination="getList" />
  </div>
</template>

<script>
import { fetchAll, fetchList, deleteDictionary } from '@/api/sys/dictionary'
import { getSorts } from '@/utils/index'
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination

export default {
  name: 'DictList',
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      tableKey: 0,
      showRows: {
        code: true,
        textColor: true,
        reserved1: true,
        reserved2: false,
        enabled: false,
        defaulted: false
      },
      list: null,
      total: 0,
      sort: {},
      listLoading: true,
      listQuery: {
        text: undefined,
        description: undefined,
        dictGroup: undefined,
        current: 1,
        size: 10,
        asc: undefined,
        desc: undefined
      },
      dictGroups: []
    }
  },
  created() {
    this.getList()
    this.fetchDictGroups()
  },
  methods: {
    fetchDictGroups() {
      fetchAll({ uid: 0, dictGroup: 'dict_group' }).then(response => {
        this.dictGroups = response.result
      })
    },
    getList() {
      this.listLoading = true
      fetchList(this.listQuery)
        .then(response => {
          this.list = response.result.list
          this.total = response.result.total
          this.listLoading = false
        })
        .catch(() => {
          this.listLoading = false
        })
    },
    handleFilter() {
      this.listQuery.current = 1
      this.getList()
    },
    sortChange(data) {
      const { prop, order } = data
      this.sort[prop] = undefined
      if (order) {
        this.sort[prop] = order !== 'descending'
      }
      const sort = getSorts(this.sort)
      this.listQuery.asc = sort.asc
      this.listQuery.desc = sort.desc
      this.handleFilter()
    },
    handleDelete(id) {
      this.$confirm('Confirm delete record?', 'Warning', {
        confirmButtonText: 'Confirm',
        cancelButtonText: 'Cancel',
        type: 'warning'
      }).then(() => {
        deleteDictionary(id).then(response => {
          console.log(response)
          this.$message({
            type: 'success',
            message: 'deleted successfully!'
          })
          this.getList()
        })
      })
    }
  }
}
</script>

<style scoped>
.edit-input {
  padding-right: 100px;
}
.cancel-btn {
  position: absolute;
  right: 15px;
  top: 10px;
}
</style>
